#include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
#include<unordered_map>
using namespace std;

unordered_map<int, vector<int>> ma;
int ans = 1;

void dfs(int pos, int pre, int head, vector<vector<int>>& fa)
{
    fa[head][pos] = pre;
    for (auto& x : ma[pos])
    {
        if (x == pre) continue;
        dfs(x, pos, head, fa);
    }
}

int dfs2(int i, int j, vector<char>& arr, vector<vector<int>>& fa, vector<vector<int>>& dp, vector<vector<bool>>& flag)
{
    if (flag[i][j]) return dp[i][j];
    if (i == j) return 1;
    if (fa[i][j] == i)
    {
        if (arr[i] == arr[j])
        {
            ans = max(ans, 2); return 2;
        }
        else return 1;
    }
    if (arr[i] == arr[j]) dp[i][j] = dfs2(fa[j][i], fa[i][j], arr, fa, dp, flag) + 2;
    else dp[i][j] = max(dfs2(i, fa[i][j], arr, fa, dp, flag), dfs2(fa[j][i], j, arr, fa, dp, flag));
    flag[i][j] = 1;
    ans = max(ans, dp[i][j]);
    return dp[i][j];
}

void solve()
{
    int n; cin >> n;
    vector<char> arr(n + 5, 0);
    vector<vector<int>> fa(n + 5, vector<int>(n + 5, 0)), dp(n + 5, vector<int>(n + 5, 0));
    vector<vector<bool>> flag(n + 5, vector<bool>(n + 5, 0));
    for (int i = 1; i <= n; i++) cin >> arr[i];
    for (int i = 1; i < n; i++)
    {
        int a, b; cin >> a >> b;
        ma[a].push_back(b);
        ma[b].push_back(a);
    }
    for (int i = 1; i <= n; i++) dfs(i, i, i, fa);
    for (int i = 1; i <= n; i++)
    {
        for (int j = i; j <= n; j++) dfs2(i, j, arr, fa, dp, flag);
    }
    cout << ans << endl;
    ma.clear();
    ans = 1;
}

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int t; cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}